<?php
namespace app\common\model;

use think\Model;

/**
 * 财务软件配置模型
 * Class AdminUser
 * @package app\common\model
 */
class FinancialSoftware extends Model
{
    /**
     * 查询一条用户信息
     *
     * @param array $params
     * @param bool $fields
     * @param bool $need_array
     * @return array
     */
    public function getInfo(array $params, $fields = true, $need_array = false)
    {
        $where = $return = [];

        if (isset($params['id'])) {
            $where['id'] = $params['id'];
        }

        if (isset($params['software_name']) && !empty($params['software_name'])) {
            $where['software_name'] = $params['software_name'];
        }

        if (!empty($where)) {
            if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
                $where['is_delete'] = $params['is_delete'];
            } else {
                $where['is_delete'] = 0;
            }

            $return = $this->where($where)->field($fields)->find();
            if (!empty($return) && $need_array) {
                $return = $return->toArray();
            }
        }

        return $return;
    }

    /**
     * @param $id
     * @param array $req
     * @return bool
     */
    public function saveDate($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $this->data = [];
        $data = $req;
        $res = $this->save($data, ['id' => $id]);
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * @param array $ids
     * @param array $req
     * @return bool
     */
    public function saveAllDate($ids, array $req)
    {
        if (empty($ids)) {
            return false;
        }

        $where = [
            'id' => ['in', $ids],
        ];

        $this->data = [];
        $data = $req;
        $res = $this->save($data, $where);
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * @param array $data
     * @return bool|mixed
     */
    public function addData(array $data)
    {
        $res = $this->allowField(true)->data($data, true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @param bool $is_need_arr
     * @return mixed
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true, $is_need_arr = false)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['software_name']) && !empty($params['software_name'])) {
            $where['group_name'] = ['like', '%' . $params['group_name'] . '%'];
        }

        if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
            $where['is_delete'] = $params['is_delete'];
        } else {
            $where['is_delete'] = 0;
        }

        if (isset($params['order_str'])) {
            $order = $params['order_str'];
        } else {
            $order = 'id asc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit);

        return $is_need_arr ? $data->toArray() : $data;
    }
}